package com.iamteer.day15;

import java.util.LinkedList;
import java.util.Random;
/**
 生成一副扑克牌，然后实现洗牌功能
 */


class Poker{
	public static final String[] SUITS = {"♠", "♥", "♣", "♦"};
	public static final String[] NUMBERS = 
			{"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
	
	String suit;
	String number;
	
	public Poker(String suit, String number) {
		this.suit = suit;
		this.number = number;
	}
/*	
	public static Poker[] newSuit() {
		Poker[] pokers = new Poker[52];
		int index = 0;
		for (int i = 0; i < Poker.SUITS.length; i++) {
			for (int j = 0; j < Poker.NUMBERS.length; j++) {
				pokers[index++] = new Poker(Poker.SUITS[i], Poker.NUMBERS[j]);
			}
		}
		return pokers;
	}
	
	public static void shuffle(Poker[] pokers) {
		Random random = new Random();
		int round = random.nextInt(50);
		int exchange;
		
		for (int i = 0; i < round; i++) {
			exchange = random.nextInt(52);
			Poker poker = pokers[exchange];
			pokers[exchange] = pokers[51-exchange];
			pokers[51-exchange] = poker;
		}
	}
	
	public static void show(Poker[] pokers) {
		for (int i = 0; i < pokers.length;) {
			System.out.print(pokers[i++] + " ");
			if(i % 13 == 0){
				System.out.println();
			}
		}
	}
*/	
	@Override
	public String toString() {
		return this.suit + this.number;
	}
}

public class PokerGame {

	public static void main(String[] args) {
/*		
		Poker[] pokers = Poker.newSuit();
		Poker.show(pokers);
		
		System.out.println("----------------------------------------");
		
		Poker.shuffle(pokers);
		Poker.show(pokers);
*/		
		LinkedList<Poker> pokers = createPoker();
		showPoker(pokers);
		
		System.out.println("----------------------------------------");
		
		shufflePoker(pokers);
		showPoker(pokers);
	}
	
	public static LinkedList<Poker> createPoker() {
		LinkedList<Poker> pokers = new LinkedList<Poker>();
		int index = 0;
		for (int i = 0; i < Poker.SUITS.length; i++) {
			for (int j = 0; j < Poker.NUMBERS.length; j++) {
				pokers.add(new Poker(Poker.SUITS[i], Poker.NUMBERS[j]));
			}
		}
		return pokers;
	}
	
	public static void showPoker(LinkedList<Poker> pokers) {
		for (int i = 0; i < pokers.size(); i++) {
			System.out.print(pokers.get(i) + "\t");
			if(i % 13 == 12){
				System.out.println();
			}
		}
	}
	
	public static void shufflePoker(LinkedList<Poker> pokers) {
		Random random = new Random();
		int round = 50 + random.nextInt(50);
		
		for (int i = 0; i < round; i++) {
			int index1 = random.nextInt(pokers.size());
			int index2 = random.nextInt(pokers.size());
			
			Poker poker1 = pokers.get(index1);
			Poker poker2 = pokers.get(index2);
			
			pokers.set(index1, poker2);
			pokers.set(index2, poker1);
		}
	}
}
